﻿#region Copyright & licence

// This file is part of Refix.
// 
// Refix is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
// 
// Refix is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Lesser General Public License for more details.
// 
// You should have received a copy of the GNU Lesser General Public
// License along with Refix.  If not, see <http://www.gnu.org/licenses/>.
// 
// Copyright (C) 2010-11 David Musgrove and others.

#endregion

using System;
using System.Collections.Generic;
using System.IO;
using System.Text;

using NUnit.Framework;

using Refix.Core.Commands;
using Refix.Core.Options;

namespace Refix.Core.Tests.Commands
{
    [TestFixture]
    public class ResolveTest : TestSolutionFolderTest
    {
        [Test]
        public void Resolve_Fails_For_Unmodified_Test_Solution()
        {
            var builder = new StringBuilder();
            OutputWriter.Writer = new StringWriter(builder);
            OutputWriter.Verbository = OutputVerbosity.Verbose;

            var queue = new Queue<string>();
            queue.Enqueue("clean");
            queue.Enqueue("-a");
            queue.Enqueue("Refix.TestSolution.sln");
            Command clean = new CommandWithOptions(queue).Command;
            clean.Validate();
            clean.Execute();

            queue.Clear();
            queue.Enqueue("res");
            queue.Enqueue("-f");
            queue.Enqueue("Refix.TestSolution.sln");
            Command resolve = new CommandWithOptions(queue).Command;
            resolve.Validate();
            Assert.False(resolve.Execute());

            queue.Clear();
            queue.Enqueue("clean");
            queue.Enqueue("-a");
            queue.Enqueue("Refix.TestSolution.sln");
            clean = new CommandWithOptions(queue).Command;
            clean.Validate();
            clean.Execute();

            Console.Write(builder.ToString());
        }

        [Test]
        public void Resolve_Shows_Graph_For_Conflicts_If_Option_Set()
        {
            var builder = new StringBuilder();
            OutputWriter.Writer = new StringWriter(builder);

            var clean = new CommandWithOptions(new[] {"clean", "-a", "Refix.TestSolution.sln"});
            clean.Command.Validate();
            clean.Command.Execute();

            var resolve = new CommandWithOptions(new[] { "res", "-f", "Refix.TestSolution.sln" });
            resolve.Command.Validate();
            resolve.Command.Execute();

            StringAssert.DoesNotContain("├", builder.ToString(),
                "Graph output should not be included unless Graph option is set.");

            builder = new StringBuilder();
            OutputWriter.Writer = new StringWriter(builder);

            resolve.Options.Add(new Graph());
            resolve.Command.Execute();

            StringAssert.Contains("├", builder.ToString(),
                                  "Graph output should be included if Graph option is set.");
        }

        [Test]
        public void Resolve_Shows_Graph_For_Non_Conflicts_If_Option_Set()
        {
            var builder = new StringBuilder();
            OutputWriter.Writer = new StringWriter(builder);

            var clean = new CommandWithOptions(new[] {"clean", "-a", "Refix.TestSolution.sln"});
            clean.Command.Validate();
            clean.Command.Execute();

            new CommandWithOptions(new[] {"alt", "-f", "Refix.Test.A", "0.1.0.2", "0.1.0.2+"});

            var resolve = new CommandWithOptions(new[] {"res", "-f", "Refix.TestSolution.sln"});
            resolve.Command.Validate();
            resolve.Command.Execute();

            StringAssert.DoesNotContain("├", builder.ToString(),
                "Graph output should not be included unless Graph option is set.");

            builder = new StringBuilder();
            OutputWriter.Writer = new StringWriter(builder);

            resolve.Options.Add(new Graph());
            resolve.Command.Execute();

            StringAssert.Contains("├", builder.ToString(),
                                  "Graph output should be included if Graph option is set.");

            new CommandWithOptions(new[] { "alt", "-fd", "Refix.Test.A", "0.1.0.2", "0.1.0.2+" });
        }

        [Test]
        public void Resolve_Passes_When_Alternative_Version_Set()
        {
            var builder = new StringBuilder();
            OutputWriter.Writer = new StringWriter(builder);
            OutputWriter.Verbository = OutputVerbosity.Verbose;

            var queue = new Queue<string>();
            queue.Enqueue("clean");
            queue.Enqueue("-a");
            queue.Enqueue("Refix.TestSolution.sln");
            Command clean = new CommandWithOptions(queue).Command;
            clean.Validate();
            clean.Execute();

            queue.Clear();
            queue.Enqueue("alt");
            queue.Enqueue("-f");
            queue.Enqueue("Refix.Test.A");
            queue.Enqueue("0.1.0.2");
            queue.Enqueue("0.1.0.2+");
            Command alternative = new CommandWithOptions(queue).Command;
            alternative.Validate();
            alternative.Execute();

            queue.Clear();
            queue.Enqueue("res");
            queue.Enqueue("-f");
            queue.Enqueue("Refix.TestSolution.sln");
            Command resolve = new CommandWithOptions(queue).Command;
            resolve.Validate();
            Assert.True(resolve.Execute());

            queue.Clear();
            queue.Enqueue("alt");
            queue.Enqueue("-fd");
            queue.Enqueue("Refix.Test.A");
            queue.Enqueue("0.1.0.2");
            queue.Enqueue("0.1.0.2+");
            alternative = new CommandWithOptions(queue).Command;
            alternative.Validate();
            alternative.Execute();

            queue.Clear();
            queue.Enqueue("clean");
            queue.Enqueue("-a");
            queue.Enqueue("Refix.TestSolution.sln");
            clean = new CommandWithOptions(queue).Command;
            clean.Validate();
            clean.Execute();

            Console.Write(builder.ToString());
        }
    }
}